package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventBfRecord;
import com.ruoyi.event.domain.bo.EventBfRecordBo;
import com.ruoyi.event.domain.vo.EventBfRecordVo;
import com.ruoyi.event.mapper.EventBfRecordMapper;
import com.ruoyi.event.service.IEventBfRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 帮扶情况Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventBfRecordServiceImpl implements IEventBfRecordService {

    private final EventBfRecordMapper baseMapper;

    /**
     * 查询帮扶情况
     */
    @Override
    public EventBfRecordVo queryById(Long bfRecordId) {
        return baseMapper.selectVoById(bfRecordId);
    }

    /**
     * 查询帮扶情况列表
     */
    @Override
    public TableDataInfo<EventBfRecordVo> queryPageList(EventBfRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventBfRecord> lqw = buildQueryWrapper(bo);
        Page<EventBfRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询帮扶情况列表
     */
    @Override
    public List<EventBfRecordVo> queryList(EventBfRecordBo bo) {
        LambdaQueryWrapper<EventBfRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventBfRecord> buildQueryWrapper(EventBfRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventBfRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventBfRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getMemberId() != null, EventBfRecord::getMemberId, bo.getMemberId());
        lqw.eq(bo.getBfsj() != null, EventBfRecord::getBfsj, bo.getBfsj());
        lqw.eq(StringUtils.isNotBlank(bo.getBfgy()), EventBfRecord::getBfgy, bo.getBfgy());
        lqw.eq(StringUtils.isNotBlank(bo.getBfnr()), EventBfRecord::getBfnr, bo.getBfnr());
        lqw.eq(StringUtils.isNotBlank(bo.getImg()), EventBfRecord::getImg, bo.getImg());
        return lqw;
    }

    /**
     * 新增帮扶情况
     */
    @Override
    public Boolean insertByBo(EventBfRecordBo bo) {
        EventBfRecord add = BeanUtil.toBean(bo, EventBfRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBfRecordId(add.getBfRecordId());
        }
        return flag;
    }

    /**
     * 修改帮扶情况
     */
    @Override
    public Boolean updateByBo(EventBfRecordBo bo) {
        EventBfRecord update = BeanUtil.toBean(bo, EventBfRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventBfRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除帮扶情况
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
